using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TVT.UI.Menu;
using System.Collections.Generic;

namespace TVT.UI
{
    public partial class MasterPage : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Menu.Menu menu = Menu.Menu.Deserialize();
            BuildMenu(menu);
        }

        private bool ValidateUser (bool canSee, List<Role> roleList)
        {
            foreach (Role item in roleList)
            {
                if (Roles.IsUserInRole(item.Name))
                {
                    canSee = true;
                    break;
                }
            }
            return canSee;
        }

        private void BuildMenu(Menu.Menu menu)
        {
            int cont = 1;
            
            foreach (Tab tab in menu.Tabs)
            {                                
                bool canSeeTab = false;

                //Validate if user is in tab.roles THEN SHOW OR NOT TAB
                canSeeTab = ValidateUser(canSeeTab, tab.Roles);

                if( canSeeTab)
                {
                    HtmlTableCell tdMenu = new HtmlTableCell();
                    tdMenu.ID = "tdMenu" + cont;

                    string varName = "Menu" + cont.ToString();
                    Table table1 = new Table();
                    table1.CellPadding = 0;
                    table1.CellSpacing = 0;
                    table1.ID = varName;

                    table1.Attributes.Add("onmouseover", "SelectedMenu('" + "Menu" + cont.ToString() + "')");
                    table1.Attributes.Add("onmouseout", "UnselectMenu();");

                    TableRow headerRow = new TableRow();
                    TableCell headerCell = new TableCell();
                    
                    headerCell.Style.Add(HtmlTextWriterStyle.Width, "120px");
                    headerCell.Style.Add(HtmlTextWriterStyle.TextAlign, "left");

                    if (tab.Link.Url == "")
                    {
                        Image img = new Image();
                        img.ImageUrl = tab.Image;
                        img.ID = "img" + varName;
                        img.BorderWidth = 0;
                        headerCell.Controls.Add(img);
                    }
                    else
                    {
                        ImageButton img = new ImageButton();
                        img.ImageUrl = tab.Image;
                        if (tab.Link.IsSilverlightPage)
                        {
                            img.Attributes.Add("onClick", tab.Link.basicSilverlightUrl + tab.Link.Url + "', '" + tab.Link.SilverlightName + tab.Link.silverlightPopUpConfiguration);
                        }
                        else
                        {
                            img.PostBackUrl = tab.Link.Url;
                        }
                        img.ID = "img" + varName;
                        img.BorderWidth = 0;
                        headerCell.Controls.Add(img);
                    }

                    int cont2 = 1;
                    
                    TableRow rowMenu = new TableRow();

                    rowMenu.ID = "Sub" + varName;
                    //set row style
                    rowMenu.Style.Add(HtmlTextWriterStyle.Display, "none");
                    rowMenu.Style.Add(HtmlTextWriterStyle.Position, "absolute");
                    rowMenu.Style.Add(HtmlTextWriterStyle.ZIndex, "1");
                    rowMenu.Style.Add(HtmlTextWriterStyle.Top, "69px");

                    foreach (TabItem tabItem in tab.TabItems)
                    {
                        
                        bool canSeeTabItem = false;
                        //Validate if user is in tabiTEM.roles THEN SHOW OR NOT TABITEM
                        canSeeTabItem = ValidateUser(canSeeTabItem, tabItem.Roles);

                        if (canSeeTabItem)
                        {
                            TableCell tcMenu = new TableCell();
                            TableCell tcMenu3 = new TableCell();
                            int pixelTop = 42 * (cont2 - 1);

                            //style image points
                            tcMenu3.Style.Add(HtmlTextWriterStyle.Top, (pixelTop + 40).ToString() + "px");
                            tcMenu3.Style.Add(HtmlTextWriterStyle.Width, "181px");
                            tcMenu3.Style.Add(HtmlTextWriterStyle.BackgroundImage, "url(App_Themes/NewDesign/Images/puntito-subm.gif)");
                            tcMenu3.CssClass = "repeatPoints";
                            tcMenu3.Text = "&nbsp;";

                            //style and attributes of the menu
                            tcMenu.ID = "ItemSub" + varName + "." + cont2.ToString();
                            tcMenu.CssClass = "itemMenu";
                            tcMenu.Attributes.Add("onmousemove", "SelectedSubMenu('Sub" + varName + "." + cont2 + "','" + cont + ".')");
                            tcMenu.Style.Add(HtmlTextWriterStyle.Width, "180px");
                            tcMenu.Style.Add(HtmlTextWriterStyle.Position, "absolute");
                            tcMenu.Style.Add(HtmlTextWriterStyle.Top, (pixelTop).ToString() + "px");


                            if (tabItem.Link.Url == "")
                            {
                                Label lbl = new Label();
                                Image img = new Image();
                                img.ImageUrl = tabItem.Image;
                                img.ID = "imgSub" + varName + "." + cont2;
                                img.BorderWidth = 0;
                                img.CssClass = "imageMenu";
                                lbl.Text = tabItem.Name;
                                lbl.CssClass = "menuText";
                                lbl.Style.Add(HtmlTextWriterStyle.Cursor, "hand");
                                img.Style.Add(HtmlTextWriterStyle.Cursor, "hand");
                                
                                lbl.Style.Add(HtmlTextWriterStyle.TextDecoration, "none");
                                tcMenu.Controls.Add(img);
                                tcMenu.Controls.Add(lbl);
                            }
                            else
                            {
                                ImageButton img = new ImageButton();
                                LinkButton btnText = new LinkButton();
                                img.ImageUrl = tabItem.Image;

                                if (tabItem.Link.IsSilverlightPage)
                                {
                                    img.Attributes.Add("onClick",tabItem.Link.basicSilverlightUrl + tabItem.Link.Url + "', '" + tabItem.Link.SilverlightName + tabItem.Link.silverlightPopUpConfiguration);
                                    btnText.Attributes.Add("onClick", tabItem.Link.basicSilverlightUrl + tabItem.Link.Url + "', '" + tabItem.Link.SilverlightName + tabItem.Link.silverlightPopUpConfiguration);
                                }
                                else
                                {
                                    img.PostBackUrl = tabItem.Link.Url;
                                    btnText.PostBackUrl = tabItem.Link.Url;
                                }

                                img.ID = "imgSub" + varName + "." + cont2;
                                img.BorderWidth = 0;
                                img.CssClass = "imageMenu";
                                
                                btnText.Text = tabItem.Name;
                                btnText.CssClass = "menuText";
                                btnText.Style.Add(HtmlTextWriterStyle.Cursor, "hand");
                                img.Style.Add(HtmlTextWriterStyle.Cursor, "hand");
                                btnText.Style.Add(HtmlTextWriterStyle.TextDecoration, "none");
                                
                                tcMenu.Controls.Add(img);
                                tcMenu.Controls.Add(btnText);
                            }

                            rowMenu.Cells.Add(tcMenu);
                            rowMenu.Cells.Add(tcMenu3);
                            
                            cont2++;
                        }

                    }
                    if (tab.TabItems.Count > 0)
                    {
                        tdMenu.Attributes.Add("onmouseover", "VisibleMenu('Sub" + varName + "');");
                        tdMenu.Attributes.Add("onmouseout", "InvisibleMenu('Sub" + varName + "');");
                        table1.Rows.Add(rowMenu);
                    }
                    
                    headerRow.Controls.Add(headerCell);
                    table1.Controls.Add(headerRow);
                    tdMenu.Controls.Add(table1);
                    trTable.Cells.Insert(1 + cont,tdMenu);
                    cont++;
                }
            }
        }
    }
}
